const MyPromise = require('./手写promise(核心功能).js')
let promise = new MyPromise((resolve, reject) => {
  // setTimeout(()=>{
  resolve('一--成功')
  // },1000)
  // reject('失败')
})

// 二，链式调用
function other () {
  return new MyPromise((resolve, reject) => {
    resolve('other')
  })
}
promise.then(value => {
  console.log(value)
  return other()
}, reason => {
  console.log(reason)
})
//三，当.then里面返回实例本身时，代码会报错。然后就会走reject
let p1 = promise.then((value) => {
  console.log(value)
  return p1
})
p1.then(value => {
  console.log(value)
}, reason => {
  console.log(reason.message)
})
//四，捕获执行器的异常
let errPromise = new MyPromise((resolve, reject) => {
  throw new Error('executor error')
  resolve('四--成功')
})
errPromise.then((value) => {
  console.log(value)
}, reason => {
  console.log(reason.message)
})
//五，捕获then里面的异常
let thenErrPromise = new MyPromise((resolve, reject) => {
  resolve('四--成功')
})
thenErrPromise.then((value) => {
  console.log(value)
  throw new Error('then error')
})
  .then((value) => {
    console.log(value)
  }, reason => {
    console.log('进入then的reject中')
    console.log(reason.message)
  })


//六，捕获失败异常  
let rejectPromise = new MyPromise((resolve, reject) => {
  reject('失败')
})
rejectPromise.then((value) => {
  console.log(value)
}, reason => {
  console.log(reason)
  return 10000
})
  .then((value) => {
    console.log(value)
  })
//七，promise内部使用异步代码
let resolvePromise = new MyPromise((resolve, reject) => {
  setTimeout(() => {
    resolve('七--成功')
  }, 3000)
})
resolvePromise
  .then(value => {
    console.log(value)
    return 'aaaaa'
  }, reason => {
    console.log(reason)
    return 5000
  })
  .then(value => {
    console.log(value)
  })
//八，then方法的参数可选，也就是参数不传递也能一次传递到后面。
let choosablePromise1 = new MyPromise((resolve, reject) => {
  resolve('八--成功')
})
choosablePromise1
  .then()
  .then()
  .then(value => { console.log(value) })
let choosablePromise2 = new MyPromise((resolve, reject) => {
  resolve('八--失败')
})
choosablePromise2
  .then()
  .then()
  .then(value => { console.log(value) }, reason => { console.log(reason) })